<?php
/*
File: stats_customers.php
Version: 2.0
Date created: 
Last Edited : 26-07-2008
Author: Craig G Smith<craig@joomla-host.co.za
*/

  defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }


  $action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
   //BOF Mod djlounge
   require ( DIR_WS_LANGUAGES . $language . '/customer_stats.php');
   //EOF Mod djlounge
# Begin class definition
class creport {
	var $begindate, $enddate, $globalstartdate, $globalenddate, $mode, $numrecords;
	var $values = array();
	function creport () {
	  $firstQuery = tep_db_query("select UNIX_TIMESTAMP(min(customers_info_date_account_created)) as first FROM " . TABLE_CUSTOMERS_INFO);
      $first = tep_db_fetch_array($firstQuery);
	  
	  $this->globalstartdate = mktime(0, 0, 0, date("m", $first['first']), date("d", $first['first']), date("Y", $first['first']));
	  $this->begindate = $this->globalstartdate;	 
	  $this->globalenddate = mktime(0, 0, 0, date("m", time()), date("d", time()), date("Y", time()));
	  $this->enddate = $this->globalenddate;
	  $this->enddateclean = date("F j, Y", $this->enddate);
	  
	  $timeframequery = "SELECT * FROM " . TABLE_CUSTOMERS_INFO . " WHERE UNIX_TIMESTAMP(`customers_info_date_account_created`) >= '".$this->globalstartdate."' AND UNIX_TIMESTAMP(`customers_info_date_account_created`) <= '".$this->enddate."'";
	  $timeframearray = tep_db_query($timeframequery);
	  $this->numrecords = mysql_num_rows($timeframearray);	
	}
	
	function getnext() {
	if ($this->begindate < $this->globalstartdate) {
        $this->begindate = $this->globalstartdate;
      } 
	  switch ($this->mode) {
        // yearly
        case '1':
          $sd = $this->begindate;
          $ed = mktime(0, 0, 0, date("m", $sd), date("d", $sd), date("Y", $sd) + 1);
          break;
        // monthly
        case '2':
          $sd = $this->begindate;
          $ed = mktime(0, 0, 0, date("m", $sd) + 1, 1, date("Y", $sd));
          break;
        // weekly
        case '3':
          $sd = $this->begindate;
          $ed = mktime(0, 0, 0, date("m", $sd), date("d", $sd) + 7, date("Y", $sd));
          break;
        // daily
        case '4':
          $sd = $this->begindate;
          $ed = mktime(0, 0, 0, date("m", $sd), date("d", $sd) + 1, date("Y", $sd));
          break;
		default:
          $sd = $this->begindate;
          $ed = mktime(0, 0, 0, date("m", $sd), date("d", $sd) + 7, date("Y", $sd));
		  break;
			
      }//end switch
	  if ($this->enddate > $this->globalenddate) $this->enddate = $this->globalenddate; 
      if ($ed > $this->enddate) $ed = $this->enddate;
	  	  
	  $timeframequery = "SELECT * FROM " . TABLE_CUSTOMERS_INFO . " WHERE UNIX_TIMESTAMP(`customers_info_date_account_created`) >= '".$sd."' AND UNIX_TIMESTAMP(`customers_info_date_account_created`) <= '".$ed."'";
	  $this->begindate = $ed;	  
	  $timeframearray = tep_db_query($timeframequery);
	  $this->numrecords = mysql_num_rows($timeframearray);
	  $this->values[] = $this->numrecords;
	  echo '<tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'"><td width="125px" align="left" class="dataTableContent">'.strftime(DATE_FORMAT_LONG, $sd).'</td><td width="125px" align="left" class="dataTableContent">'. strftime(DATE_FORMAT_LONG, $ed).'</td><td class="dataTableContent"><b>'.$this->numrecords.'</b></td></tr>';
	}# end getnext
}# end creport class

# Create a new instance of the creport class
$report = new creport; 

	if ( $_POST['mode'] ) 
	{    
		$mode = $_POST['mode'];
  	}
  	if ($mode < 1 || $mode > 4) {
    	$mode = 2;
  	}
	  if ($_POST['startday'] && $_POST['startmonth'] && $_POST['startyear']){
	  $report->begindate = mktime(0, 0, 0, $_POST['startmonth'], $_POST['startday'], $_POST['startyear']); }
if ($_POST['endday'] && $_POST['endmonth'] && $_POST['endyear']){
	  $report->enddate = mktime(0, 0, 0, $_POST['endmonth'], $_POST['endday'], $_POST['endyear']); }
	$report->mode = $mode;
?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">

		<?php require(DIR_WS_INCLUDES . 'header.php'); ?>

<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr>
    	<td mmTranslatedValueHiliteColor="HILITECOLOR=%22Dyn%20Untranslated%20Color%22" mmTranslatedValueDynAttrs="DynAttrs=width" width="<?php echo BOX_WIDTH; ?>" valign="top">
			<table mmTranslatedValueHiliteColor="HILITECOLOR=%22Dyn%20Untranslated%20Color%22" mmTranslatedValueDynAttrs="DynAttrs=width" border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
		<!-- left_navigation //-->
		<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
		<!-- left_navigation_eof //-->
      		</table>
		</td>
	  <td valign="top" class"menuBoxHeading">
		<p class="pageHeading"><?php  echo HEADING_TITLE;?></p>
		<p class="main"><?php  echo TEXT_DATAFROM;?><b><?php  echo strftime(DATE_FORMAT_LONG, $report->begindate); ?></b><?php  echo TEXT_TO;?><b><?php  echo strftime(DATE_FORMAT_LONG, $report->enddate); ?></b></p>
		<p class="menuBoxHeading"><?php  echo TEXT_TOTAL_ACCOUNTS;?><b><?php  echo $report->numrecords; ?></b></p>		
<form name="options" method="post" action="<?php echo tep_href_link('customer_stats.php'); ?>" class="menuBoxHeading">
	  <table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
		<td valign="top" width="125px">
		
		  <input name="mode" type="radio" value="1" <?php if ($mode == 1) echo "checked"; ?>><?php  echo TEXT_YEARLY;?><br>
		  <input name="mode" type="radio" value="2" <?php if ($mode == 2) echo "checked"; ?>><?php  echo TEXT_MONTHLY;?><br>
		  <input name="mode" type="radio" value="3" <?php if ($mode == 3) echo "checked"; ?>><?php  echo TEXT_WEEKLY;?><br>
		  <input name="mode" type="radio" value="4" <?php if ($mode == 4) echo "checked"; ?>><?php  echo TEXT_DAILY;?><br>
          </td>
				<td valign="top">
			  <table width="100%" border="0" cellpadding="0" cellspacing="0">
				<tr>
				  <td>
				  	<b><?php  echo TEXT_BEGINDATE;?></b> (d m Y)<br>
					<input name="startmonth" type="text" value="<?php echo date("n", $report->begindate); ?>" size="2" maxlength="2">
					<input name="startday" type="text" size="2" maxlength="2" value="<?php echo date("j", $report->begindate); ?>">
					<input name="startyear" type="text" size="4" maxlength="4" value="<?php echo date("Y", $report->begindate); ?>">			       
				   </td>
				</tr>
				<tr>
				<td><br>
				  	<b><?php  echo TEXT_ENDDATE;?></b> (d m Y)<br>
					<input name="endmonth" type="text" value="<?php echo date("n", $report->enddate); ?>" size="2" maxlength="2">
					<input name="endday" type="text" value="<?php echo date("j", $report->enddate); ?>" size="2" maxlength="2">
					<input name="endyear" type="text" size="4" value="<?php echo date("Y", $report->enddate); ?>"  maxlength="4">
					 <input type="submit" name="Submit" value="<?php echo TEXT_SUBMIT; ?>"></form>	
				</td>
				</tr>
				</table>

				</td>
				</tr></table>
<br>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="adminlist"><thead>
	<tr class="dataTableHeadingRow"><th width="220px" align="left" class="dataTableHeadingContent"><?php  echo TEXT_BEGINDATE;?></td><th width="220px" align="left" class="dataTableHeadingContent"><?php  echo TEXT_ENDDATE;?></td><th align="left" class="dataTableHeadingContent"><?php  echo TEXT_NUMBER_OF_ACCOUNTS_CREATED;?></td></tr></thead>
<?php

while ($report->begindate < $report->enddate) {
  $report->getnext();
  }
?>  
   
   </table>
   </td></tr></table>
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
?>